package com.ruoyi.laiwu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.laiwu.domain.bo.SyncConfigBo;
import com.ruoyi.laiwu.domain.vo.SyncConfigVo;
import com.ruoyi.laiwu.domain.SyncConfig;
import com.ruoyi.laiwu.mapper.SyncConfigMapper;
import com.ruoyi.laiwu.service.ISyncConfigService;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数据同步配置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@RequiredArgsConstructor
@Service
public class SyncConfigServiceImpl implements ISyncConfigService {

    private final SyncConfigMapper baseMapper;

    /**
     * 查询数据同步配置
     */
    @Override
    public SyncConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询数据同步配置列表
     */
    @Override
    public TableDataInfo<SyncConfigVo> queryPageList(SyncConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SyncConfig> lqw = buildQueryWrapper(bo);
        Page<SyncConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询数据同步配置列表
     */
    @Override
    public List<SyncConfigVo> queryList(SyncConfigBo bo) {
        LambdaQueryWrapper<SyncConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SyncConfig> buildQueryWrapper(SyncConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SyncConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SyncConfig::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getModelClass()), SyncConfig::getModelClass, bo.getModelClass());
        lqw.eq(bo.getStarTime() != null, SyncConfig::getStarTime, bo.getStarTime());
        lqw.eq(bo.getSyncDate() != null, SyncConfig::getSyncDate, bo.getSyncDate());
        return lqw;
    }

    /**
     * 新增数据同步配置
     */
    @Override
    public Boolean insertByBo(SyncConfigBo bo) {
        SyncConfig add = BeanUtil.toBean(bo, SyncConfig.class);
        validEntityBeforeSave(add);
        add.setStatus("not");
        add.setSyncDate(LocalDate.now());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改数据同步配置
     */
    @Override
    public Boolean updateByBo(SyncConfigBo bo) {
        SyncConfig update = BeanUtil.toBean(bo, SyncConfig.class);
        validEntityBeforeSave(update);

        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SyncConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数据同步配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
